/**
 * Copyright 2013 CoderDream's Studio
 * All right reserved.
 * Created on 2013-4-5 ����1:11:47 by pig
 */
package com.pig.phttprequest;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.TreeMap;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;

import com.pig.phttprequest.PHttpRequestCallBack.PHttpStringCallback;

/**
 * @author pig
 *
 */
public class PHttpRequestPost extends PHttpRequestModel {
	
	private PHttpStringCallback httpStringCallback;
	
	public PHttpRequestPost(String url, TreeMap<String, String> params) {
		super.requestUrl = url;
		super.requestParams = params;
	}
	
	public HttpPost initRequestMethod() {
		
		HttpPost postMethod = null;
		
		try {
			
			if(null == super.requestUrl || null == super.requestClient) {
				return postMethod;
			}
			
			postMethod = new HttpPost(super.requestUrl);
			ArrayList<BasicNameValuePair> nameValuePairs = serizlizeNameValuePairs(super.requestParams);
			
			//params
			if(nameValuePairs.size() > 0){
				postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs, super.requestEncode));
			}
			
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return postMethod;
	}
	
	/* (non-Javadoc)
	 * @see com.pig.phttprequest.PHttpRequestModel#executeRequest()
	 */
	@Override
	protected HttpResponse executeRequestMethod() {
		// TODO Auto-generated method stub
		
		HttpResponse httpResponse = null;
		
		try {
			HttpPost postMethod = this.initRequestMethod();
			if(null == postMethod) {
				return httpResponse;
			}
			
			httpResponse = super.requestClient.execute(postMethod);
			return httpResponse;
			
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}
	
	/* (non-Javadoc)
	 * @see com.pig.phttprequest.PHttpRequestModel#requestHttpResponseContentLength(long, java.io.InputStream)
	 */
	@Override
	public InputStream requestHttpResponseContentLength(long contentSize,
			InputStream inputStream) {
		// TODO Auto-generated method stub
		
		if(null != inputStream) {
			
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			byte[] buffer = new byte[BUFFER_SIZE];
			int len = 0;
			try {
				while((len = inputStream.read(buffer)) != -1) {
					baos.write(buffer, 0, len);
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			if(null != httpStringCallback) {
				httpStringCallback.requestString(baos.toString());
			}
			
		}
		
		return super.requestHttpResponseContentLength(contentSize, inputStream);
	}
	
	/**
	 * @param httpStringCallback the httpStringCallback to set
	 */
	public void setHttpStringCallback(PHttpStringCallback httpStringCallback) {
		this.httpStringCallback = httpStringCallback;
	}

	/**
	 * ��ʽ��post�ύ�Ĳ����ֵ��
	 * @param tmParams
	 * @return
	 */
	protected ArrayList<BasicNameValuePair> serizlizeNameValuePairs(TreeMap<String, String> tmParams) {
		
		ArrayList<BasicNameValuePair> nameValuePairs = new ArrayList<BasicNameValuePair>(2);
		
		if(null == tmParams || tmParams.size() < 1) {
			return nameValuePairs;
		}
		
		Iterator<String> it = tmParams.keySet().iterator();
		while(it.hasNext()) {
			String paramKey = it.next();
			String paramValue = tmParams.get(paramKey);
			nameValuePairs.add(new BasicNameValuePair(paramKey, paramValue));
		}
		
		return nameValuePairs;
	}
	
}
